//
//  PopoverAnimator.swift
//  Stauts
//
//  Created by Kylin on 16/3/14.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    // 属性
    var presentedFrame : CGRect = CGRectZero
    var isPresented :Bool = false
    var animateCallBack : ((isPresented : Bool) -> ())?
    
}

extension PopoverAnimator : UIViewControllerTransitioningDelegate {
    // 目的:改变弹出View的尺寸和添加蒙版
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let present = KylinPresentationController(presentedViewController : presented,presentingViewController: presenting)
        // 传送Frame值
        present.presentedFrame = presentedFrame
        return present
    }
    
     // 目的:弹出动画交给谁来管理
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        
        if animateCallBack != nil {
            animateCallBack!(isPresented : isPresented)
        }
        
        return self
    }
     // 目的:消失动画交给谁来管理
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        if animateCallBack != nil {
            animateCallBack!(isPresented : isPresented)
        }
        return self
    }
}

extension PopoverAnimator : UIViewControllerAnimatedTransitioning {
    // 返回动画执行的时间间隔
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    // 在该方法中有一个参数transitionContext,通过transitionContext,可以获取到弹出的View,也可以获取到消失的View
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? presentAnimate(transitionContext) :dismissAnimate(transitionContext)
    }
    
}


// MARK:- 执行消失动画
extension PopoverAnimator {
     /// 执行弹出动画
    private func presentAnimate(transitionContext : UIViewControllerContextTransitioning) {
        // 1.获取弹出的View
        let presentedView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
         // 2.自己将弹出的View添加到容器视图中
        transitionContext.containerView()?.addSubview(presentedView)
        // 执行动画
        presentedView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        
       UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
        presentedView.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            
            // 如果是自定义转场,动画结束后必须告诉转场上下文动画已经完成
            transitionContext.completeTransition(true)
        }
        
    }
    
     /// 执行消失动画
    private func dismissAnimate(transitionContext :UIViewControllerContextTransitioning) {
        
        // 1:取出消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        // 执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.0001)
            }) { (_) -> Void in
            dismissView.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
}















